import Vue from "vue";
import VueRouter from "vue-router";
import wxShare from "@/assets/js/wxShare.js";
import { myStore } from "../store";

import Login from "../views/home/login.vue";
import BindPhone from "../views/home/bindPhone.vue";
// import ChecksyStem from "../views/home/checkSystem.vue";

import CoachHome from "../views/coach/coachHome.vue";
import CoachImprove from "../views/coach/coachImprove.vue";
import CoachImproveView from "../views/coach/coachImproveView.vue";
import CoachRegister from "../views/coach/coachRegister.vue";

import RefereeHome from "../views/referee/refereeHome.vue";
import RefereeRegister from "../views/referee/refereeRegister.vue";
import RefereeRegisterView from "../views/referee/refereeRegisterView.vue";
import RefereeMemberForm from "../views/referee/refereeMemberForm.vue";
import RefereeMemberView from "../views/referee/refereeMemberView.vue";

import CoachEnrollList from "../views/enroll/coachEnrollList.vue";
import CoachEnrollDetail from "../views/enroll/coachEnrollDetail.vue";
import CoachEnrollHistoryList from "../views/enroll/coachEnrollHistoryList.vue";
import RefereeEnrollList from "../views/enroll/refereeEnrollList.vue";
import RefereeEnrollDetail from "../views/enroll/refereeEnrollDetail.vue";
import RefereeEnrollHistoryList from "../views/enroll/refereeEnrollHistoryList.vue";

import RefereeAnswerIndex from "../views/answer/refereeAnswerIndex.vue";
import RefereeAnswer from "../views/answer/refereeAnswer.vue";

import OrderSuccess from "../views/order/orderSuccess.vue";
import RefereeRegisterOrderDetail from "../views/order/refereeRegisterOrderDetail.vue";
import RefereeRegisterOrderList from "../views/order/refereeRegisterOrderList.vue";
import RefereeRegisterOrderView from "../views/order/refereeRegisterOrderView.vue";
import CoachEnrollOrderDetail from "../views/order/coachEnrollOrderDetail.vue";
import RefereeEnrollOrderDetail from "../views/order/refereeEnrollOrderDetail.vue";

import MainUserCenter from "../views/user/mainUserCenter.vue";
import CoachUserCenter from "../views/user/coachUserCenter.vue";
import RefereeUserCenter from "../views/user/refereeUserCenter.vue";
import MyHonor from "../views/user/myHonor.vue";
import UserInfo from "../views/user/userInfo.vue";
import NoInfoTips from "../views/user/noInfoTips.vue";

import Remove from "../views/home/remove.vue";

import currentGame from "../views/game/currentGame.vue";
import childrenGroup from "../views/game/childrenGroup.vue";
import childrenGroupU from "../views/game/childrenGroupU.vue";
import uDetails from "../views/game/uDetails.vue";
import uList from "../views/game/uList.vue";
import createTeam from "../views/game/createTeam.vue";
import addMember from "../views/game/addMember.vue";
import addMemberDetails from "../views/game/addMemberDetails.vue";
import chooseTeam from "../views/game/chooseTeam.vue";
import teamMassage from "../views/game/teamMassage.vue";
import applyDetails from "../views/game/applyDetails.vue";
import applySuccess from "../views/game/applySuccess.vue";
import eventImprove from "../views/game/eventImprove.vue";
import applySuccessDetails from "../views/game/applySuccessDetails.vue";
import addPlayers from "../views/game/addPlayers.vue";
import gameRules from "../views/function/gameRules.vue";
import informationSearch from "../views/function/informationSearch.vue";
import teamInformation from "../views/function/teamInformation.vue";
import watchLive from "../views/function/watchLive.vue";
import teamlist from "../views/function/Teamlist.vue";
import myApply from "../views/function/myApply.vue";
import editPlayer from "../views/game/editPlayer.vue";
import signIn from "../views/sign/signIn.vue";
import perfectReferees from "../views/sign/perfectReferees.vue";
import signList from "../views/sign/signList.vue";
import newlyadded from '../views/function/newlyadded.vue'
import editteam from '../views/function/editteam.vue'
import signinlist from '../views/function/signinlist.vue'
Vue.use(VueRouter);

// const originalPush = VueRouter.prototype.push;
// VueRouter.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch((err) => err);
// };

const routes = [
  // home
  // {
  //   path: "/",
  //   name: "ChecksyStem",
  //   component: ChecksyStem,
  //   title: "首页"
  // }, // 选择系统页
  {
    path: "/login",
    name: "Login",
    component: Login,
    meta: {
      title: "登录",
    },
  }, // 登录
  {
    path: "/bindPhone",
    name: "BindPhone",
    component: BindPhone,
  }, // 绑定手机

  /**************************************************************************** */
  /**************************************************************************** */
  /**************************************************************************** */
  /**************************************************************************** */
  //功能

  //竞赛规程
  {
    path: "/gameCenter/gameRules",
    name: "gameRules",
    component: gameRules,
    meta: {
      title: "竞赛规程",
    },
  },
  {
    path: "/gameCenter/search",
    name: "informationSearch",
    component: informationSearch,
    meta: {
      title: "信息搜索",
    },
  },
  {
    path: "/gameCenter/teamlist",
    name: "teamlist",
    component: teamlist,
    meta: {
      title: "球队信息",
    },
  },
  {
    path: "/gameCenter/teamInformation",
    name: "teamInformation",
    component: teamInformation,
    meta: {
      title: "球员信息",
    },
  },
  {
    path: "/gameCenter/newlyadded",
    name: "newlyadded",
    component: newlyadded,
    meta: {
      title: "新增队员",
    },
    // 球员信息中的新增队员
  },
  {
    path: "/gameCenter/editteam",
    name: "editteam",
    component: editteam,
    meta: {
      title: "编辑队伍",
    },
    // 球员信息中的编辑队伍
  },
  {
    path: "/gameCenter/live",
    name: "watchLive",
    component: watchLive,
    meta: {
      title: "观看直播",
    },
  },
  {
    path: "/gameCenter/myApply",
    name: "myApply",
    component: myApply,
    meta: {
      title: "我的报名",
    },
  },
  //赛事
  {
    path: "/",
    name: "gameCenter",
    component: currentGame,
    meta: {
      title: "篮球赛事",
    },
  },
  {
    path: "/gameCenter/childrenGroup",
    name: "childrenGroup",
    component: childrenGroup,
    meta: {
      title: "上冠少儿组",
    },
  },
  {
    path: "/gameCenter/childrenGroupU",
    name: "childrenGroupU",
    component: childrenGroupU,
    meta: {
      title: "u12少儿组分组",
    },
  },
  {
    path: "/gameCenter/children/list",
    name: "uList",
    component: uList,
    meta: {
      title: "",
    },
  },
  {
    path: "/gameCenter/children/details",
    name: "uDetails",
    component: uDetails,
    meta: {
      title: "篮球少儿组",
    },
  },
  {
    path: "/gameCenter/children/createTeam",
    name: "createTeam",
    component: createTeam,
    meta: {
      title: "创建队伍",
    },
  },
  {
    path: "/gameCenter/children/addMember",
    name: "addMember",
    component: addMember,
    meta: {
      title: "添加队员",
    },
  },
  {
    path: "/gameCenter/children/addMemberDetails",
    name: "addMemberDetails",
    component: addMemberDetails,
    meta: {
      title: "添加队员",
    },
  },

  {
    path: "/gameCenter/addPlayers",
    name: "addPlayers",
    component: addPlayers,
    meta: {
      title: "添加队员",
    },
    //这个是报名后操作页面后面的添加队员
  },
  {
    path: "/gameCenter/editPlayer",
    name: "editPlayer",
    component: editPlayer,
    meta: {
      title: "编辑球员",
    },
    //这个是报名后操作页面后面的编辑队员
  },
  {
    path: "/gameCenter/children/chooseTeam",
    name: "chooseTeam",
    component: chooseTeam,
    meta: {
      title: "选择队伍",
    },
  },
  {
    path: "/gameCenter/children/teamMassage",
    name: "teamMassage",
    component: teamMassage,
    meta: {
      title: "球队信息",
    },
  },
  {
    path: "/gameCenter/children/applyDetails",
    name: "applyDetails",
    component: applyDetails,
    meta: {
      title: "报名详情",
    },
  },
  {
    path: "/gameCenter/children/applySuccess",
    name: "applySuccess",
    component: applySuccess,
    meta: {
      title: "提示",
    },
  },
  {
    path: "/gameCenter/children/applySuccessDetails",
    name: "applySuccessDetails",
    component: applySuccessDetails,
    meta: {
      title: "报名详情",
    },
  },
  {
    path: "/gameCenter/improve",
    name: "eventImprove",
    component: eventImprove,
    meta: {
      title: "完善信息",
    },
  },
  {
    path: "/gameCenter/signIn",
    name: "signIn",
    component: signIn,
    meta: {
      title: "裁判员签到",
    },
  },
  {
    path: "/gameCenter/signinlist",
    name: "signinlist",
    component: signinlist,
    meta: {
      title: "签到活动列表",
    },
  },
  {
    path: "/gameCenter/perfectReferees",
    name: "perfectReferees",
    component: perfectReferees,
    meta: {
      title: "完善信息",
    },
  },
  {
    path: "/gameCenter/signList",
    name: "signList",
    component: signList,
    meta: {
      title: "签到记录",
    },
  },
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  // 教练员
  {
    path: "/coachCenter",
    name: "CoachHome",
    component: CoachHome,
    meta: {
      title: "教练员",
    },
  }, // 教练员------首页
  {
    path: "/coachCenter/improve",
    name: "CoachImprove",
    component: CoachImprove,
    meta: {
      title: "教练员",
    },
  }, // 教练员------完善信息
  {
    path: "/coachCenter/improve/view",
    name: "CoachImproveView",
    component: CoachImproveView,
  }, // 教练员------查看填写
  {
    path: "/coachCenter/register",
    name: "CoachRegister",
    component: CoachRegister,
    meta: {
      title: "注册",
    },
  }, // 教练员------注册
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  // 裁判员
  {
    path: "/refereeCenter",
    name: "RefereeHome",
    component: RefereeHome,
    meta: {
      title: "裁判员",
    },
  }, // 裁判员------首页
  {
    path: "/refereeCenter/register",
    name: "RefereeRegister",
    component: RefereeRegister,
    meta: {
      title: "裁判员",
    },
  }, // 裁判员------注册
  {
    path: "/refereeCenter/register/view",
    name: "RefereeRegisterView",
    component: RefereeRegisterView,
    meta: {
      title: "查看详情",
    },
  }, // 裁判员------注册订单填写查看
  {
    path: "/refereeCenter/member/join",
    name: "RefereeMemberForm",
    component: RefereeMemberForm,
    meta: {
      title: "会员注册",
    },
  }, // 裁判员------会员注册
  {
    path: "/refereeCenter/member/view",
    name: "RefereeMemberView",
    component: RefereeMemberView,
    meta: {
      title: "注册信息",
    },
  }, // 裁判员------会员注册
  /**************************************************************************** */
  /**************************************************************************** */
  /**************************************************************************** */
  /**************************************************************************** */
  // 报名
  {
    path: "/enroll/coach/List",
    name: "CoachEnrollList",
    component: CoachEnrollList,
    meta: {
      title: "培训报名",
    },
  }, // 教练员------培训报名列表页
  {
    path: "/enroll/coach/detail",
    name: "CoachEnrollDetail",
    component: CoachEnrollDetail,
    meta: {
      title: "报名详情",
    },
  }, // 教练员------培训报名详情介绍页
  {
    path: "/enroll/coach/history",
    name: "CoachEnrollHistoryList",
    component: CoachEnrollHistoryList,
    meta: {
      title: "我的报名",
    },
  }, // 教练员------我的报名页

  {
    path: "/enroll/referee/List",
    name: "RefereeEnrollList",
    component: RefereeEnrollList,
    meta: {
      title: "培训报名",
    },
  }, // 裁判员------培训报名列表页
  {
    path: "/enroll/referee/detail",
    name: "RefereeEnrollDetail",
    component: RefereeEnrollDetail,
    meta: {
      title: "报名详情",
    },
  }, // 裁判员------培训报名详情介绍页
  {
    path: "/enroll/referee/history",
    name: "RefereeEnrollHistoryList",
    component: RefereeEnrollHistoryList,
    meta: {
      title: "我的报名",
    },
  }, // 裁判员------我的报名页
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  // 答题
  {
    path: "/answer/referee/index",
    name: "RefereeAnswerIndex",
    component: RefereeAnswerIndex,
    meta: {
      title: "上岗认证",
    },
  }, // 裁判员------上岗认证答题前置页页
  {
    path: "/answer/referee/exam",
    name: "RefereeAnswer",
    component: RefereeAnswer,
    meta: {
      title: "上岗认证",
    },
  }, // 裁判员------上岗认证答题页
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  // 订单
  {
    path: "/order/success",
    name: "OrderSuccess",
    component: OrderSuccess,
    meta: {
      title: "提示",
    },
  }, // 订单完成提示页
  {
    path: "/order/referee/register/detail",
    name: "RefereeRegisterOrderDetail",
    component: RefereeRegisterOrderDetail,
    meta: {
      title: "支付费用",
    },
  }, // 裁判员------注册订单详情页
  {
    path: "/order/referee/register/List",
    name: "RefereeRegisterOrderList",
    component: RefereeRegisterOrderList,
    meta: {
      title: "注册记录",
    },
  }, // 裁判员------注册订单列表
  {
    path: "/order/referee/register/view",
    name: "RefereeRegisterOrderView",
    component: RefereeRegisterOrderView,
    meta: {
      title: "注册详情",
    },
  }, // 裁判员------注册详情查看
  {
    path: "/order/coach/enroll/detail",
    name: "CoachEnrollOrderDetail",
    component: CoachEnrollOrderDetail,
    meta: {
      title: "订单详情",
    },
  }, // 教练员------培训报名订单详情
  {
    path: "/order/referee/enroll/detail",
    name: "RefereeEnrollOrderDetail",
    component: RefereeEnrollOrderDetail,
    meta: {
      title: "订单详情",
    },
  }, // 裁判员------培训报名订单详情
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  // 用户
  {
    path: "/userCenter",
    name: "MainUserCenter",
    component: MainUserCenter,
    meta: {
      title: "个人中心",
    },
  }, // 总个人中心页
  {
    path: "/coachCenter/userCenter",
    name: "CoachUserCenter",
    component: CoachUserCenter,
    meta: {
      title: "个人中心",
    },
  }, // 教练员-----个人中心页
  {
    path: "/refereeCenter/userCenter",
    name: "RefereeUserCenter",
    component: RefereeUserCenter,
    meta: {
      title: "个人中心",
    },
  }, // 裁判员----个人中心页
  {
    path: "/userHonors",
    name: "MyHonor",
    component: MyHonor,
    meta: {
      title: "我的荣誉",
    },
  }, // 我的荣誉信息页
  {
    path: "/userInfo",
    name: "UserInfo",
    component: UserInfo,
    meta: {
      title: "我的信息",
    },
  }, // 我的信息选择页
  {
    path: "/noInfoTips",
    name: "NoInfoTips",
    component: NoInfoTips,
    meta: {
      title: "我的信息",
    },
  }, // 我的信息提示填写页面
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  {
    path: "/remove",
    name: "Remove",
    component: Remove,
  }, // 清除缓存
];

const router = new VueRouter({
  mode: "history",
  base: "/event/",      //正式
  // base: "/testevent/", //测试
  routes,
});

// // 获取原型对象push函数
// const originalPush = VueRouter.prototype.push

// // 获取原型对象replace函数
// const originalReplace = VueRouter.prototype.replace

// // 修改原型对象中的push函数
// VueRouter.prototype.push = function push(location){
// return originalPush.call(this , location).catch(err=>err)
// }

// // 修改原型对象中的replace函数
// VueRouter.prototype.replace = function replace(location){
// return originalReplace.call(this , location).catch(err=>err)
// }

// 全局路由守卫
router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title;
  }

  // 需要登录的页面
  const nextRoute = [
    "gameCenter",
    "signIn",
    "gameRules",
    "myApply",
    "watchLive",
    "teamlist",
    "informationSearch",


    "ChecksyStem",

    "CoachHome",
    "CoachImprove",
    "CoachImproveView",
    "CoachRegister",

    "childrenGroup",
    "childrenGroupU",
    "uList",
    "uDetails",

    "RefereeHome",
    "RefereeRegister",
    "RefereeRegisterView",
    "RefereeMemberForm",
    "RefereeMemberView",

    "CoachEnrollList",
    "CoachEnrollDetail",
    "CoachEnrollHistoryList",
    "RefereeEnrollList",
    "RefereeEnrollDetail",

    "RefereeAnswerIndex",
    "RefereeAnswer",

    "OrderSuccess",
    "RefereeRegisterOrderDetail",
    "RefereeRegisterOrderList",
    "RefereeRegisterOrderView",
    "CoachEnrollOrderDetail",

    "MainUserCenter",
    "CoachUserCenter",
    "RefereeUserCenter",
    "MyHonor",
    "UserInfo",
    "NoInfoTips",
  ];
  let isLogin = sessionStorage.getItem("COACH_LOGIN_TOKEN"); // 判断是否登录，本地存储有用户数据则视为已经登录
  let hasPhone = JSON.parse(sessionStorage.getItem("COACH_HAS_PHONE")); // 判断是否绑定手机
  // 未登录状态；当路由到 nextRoute 指定页时，跳转至 登录
  if (nextRoute.indexOf(to.name) >= 0) {
    // 检测是否登录的页面
    if (!isLogin) {
      // 如果未登录（本地存储无用户数据），并且要跳到登录页面
      if (from.name === "Login") {
        next("/login");
        return;
      } else {
        router.push({
          name: "Login",
          params: {
            redirect: to.fullPath,
          },
        });
      }
    } else {
      if (!hasPhone) {
        if (from.name === "BindPhone") {
          next("/bindPhone");
          return;
        }

        router.push({
          name: "BindPhone",
        });
      }
    }
  }
  // let before_Path = to.fullPath
  // let before_Path = router.app.$options.store.state;
  setTimeout(() => {
    let before_Path = router.app.$options.store.state.before_Path;
    // console.log(before_Path, 'setTimeout');
    if (isLogin) {
      if (to.name == before_Path) {
        router.push({
          path: before_Path,
        });
      } else {
        if (to.name == "Login") {
          next("/");
          return;
        }
      }
    }
    // if (before_Path == "/gameCenter/signIn") {
    //   if (isLogin) {
    //     router.push({
    //       name: "signIn",
    //     });
    //   }
    // } else {
    //   if (to.name === "Login") {
    //     if (isLogin) {
    //       next("/");
    //       return;
    //     }
    //   }
    // }
  }, 0);
  // 已登录状态；当路由到 登录 时，跳转至 首页
  // if (before_Path === "/gameCenter/signIn") {
  //   if (isLogin) {
  //     router.push({
  //       name: "signIn",
  //     });
  //   }
  // } else {
  //   if (isLogin) {
  //     next("/");
  //     return;
  //   }
  // }
  // if (to.name === "Login") {
  //   if (isLogin) {
  //     next("/");
  //     return;
  //   }
  // }

  next(); // 必须使用 next ,执行效果依赖 next 方法的调用参数
});

router.afterEach((to) => {
  wxShare(
    "https://shslqxh.1meeting.com/event" + to.fullPath,
    "上海市篮球协会数据服务中心",
    "",
    "上海市篮球协会数据服务中心",
    "https://shslqxh.1meeting.com/event",
    "https://shslqxh.1meeting.com/event/api/image/common/logo.png"
  );
  const u = navigator.userAgent.toLowerCase();
  if (
    u.indexOf("like mac os x") < 0 ||
    u.match(/MicroMessenger/i) != "micromessenger"
  )
    return;
  if (to.path !== global.location.pathname) {
    // console.log(to);
    // console.log(global.location.pathname);
    // location.assign("/coach" + to.fullPath);
  }
});

export default router;
